package _1Cookie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SetCookieServlet")
public class SetCookieServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		
		// 1. 创建Cookie对象
		Cookie c1 = new Cookie("username","zhangsan");
		
		// 2. 设定在客户端保留时间 秒
//		c1.setMaxAge(3*24*60*60);// 3天
		c1.setMaxAge(20);// 10s 后就失效了
		
		// 0 立即失效
		// -1 浏览器关闭失效
		
		// 3. 添加到响应对象中
		resp.addCookie(c1);
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}